# leetcode 01 两数之和 暴力解法

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                if nums[i] + nums[j] == target:
                    return [i, j]


# 哈希表解法
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        nmap = {} # {nums[i]: i}存放遍历过的元素， 相同数值存在多个，则更新为最新索引
        for i in range(len(nums)):
            match = target - nums[i] # 查询与nums[i]配对的元素是否在nmap中
            if match in nmap:
                return [i, nmap[match]]
            nmap[nums[i]] = i
        return None
